package com.ruoyi.water.controller;

import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.water.domain.MeterData;
import com.ruoyi.water.service.IMeterDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 流量数据Controller
 *
 * @author wujiong
 * @date 2025-09-13
 */
@RestController
@RequestMapping("/water/meterdata")
public class MeterDataController extends BaseController
{
    @Autowired
    private IMeterDataService meterdataService;

    /**
     * 查询流量数据列表（原有方法，保持不变）
     */
    @PreAuthorize("@ss.hasPermi('water:meterdata:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            MeterData meterdata,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime
    ) {
        startPage();
        QueryWrapper<MeterData> queryWrapper = new QueryWrapper<>(meterdata);

        if (startTime != null) {
            queryWrapper.ge("readtime", startTime);
        }
        if (endTime != null) {
            queryWrapper.le("readtime", endTime);
        }

        List<MeterData> list = meterdataService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询整点流量数据列表（新增方法）
     * 筛选整点前后5分钟内的数据
     */
    @PreAuthorize("@ss.hasPermi('water:meterdata:listhour')")
    @GetMapping("/listhour")
    public TableDataInfo listHour(
            MeterData meterdata,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime
    ) {
        startPage();
        QueryWrapper<MeterData> queryWrapper = new QueryWrapper<>(meterdata);

        // 时间范围条件
        if (startTime != null) {
            queryWrapper.ge("readtime", startTime);
        }
        if (endTime != null) {
            queryWrapper.le("readtime", endTime);
        }

        // 筛选整点前后5分钟的数据：分钟数在0-5或55-59之间
        queryWrapper.apply("MINUTE(readtime) BETWEEN 0 AND 5 OR MINUTE(readtime) BETWEEN 55 AND 59");

        // 按时间排序
        queryWrapper.orderByAsc("readtime");

        List<MeterData> list = meterdataService.list(queryWrapper);
        return getDataTable(list);
    }

    // 以下为原有方法，保持不变
    /**
     * 导出流量数据列表
     */
    @PreAuthorize("@ss.hasPermi('water:meterdata:export')")
    @Log(title = "流量数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeterData meterdata)
    {
        QueryWrapper<MeterData> queryWrapper = new QueryWrapper<>(meterdata);
        List<MeterData> list = meterdataService.list(queryWrapper);
        ExcelUtil<MeterData> util = new ExcelUtil<MeterData>(MeterData.class);
        util.exportExcel(response, list, "流量数据数据");
    }

    /**
     * 获取流量数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('water:meterdata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(meterdataService.getById(id));
    }

    /**
     * 新增流量数据
     */
    @PreAuthorize("@ss.hasPermi('water:meterdata:add')")
    @Log(title = "流量数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeterData meterdata)
    {
        return toAjax(meterdataService.save(meterdata) ? 1 : 0);
    }

    /**
     * 修改流量数据
     */
    @PreAuthorize("@ss.hasPermi('water:meterdata:edit')")
    @Log(title = "流量数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeterData meterdata)
    {
        return toAjax(meterdataService.updateById(meterdata) ? 1 : 0);
    }

    /**
     * 删除流量数据
     */
    @PreAuthorize("@ss.hasPermi('water:meterdata:remove')")
    @Log(title = "流量数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(meterdataService.removeByIds(Arrays.asList(ids)) ? ids.length : 0);
    }
}